const toString = Object.prototype.toString

/**
 * 判断是否为指定类型
 * @param val 
 * @param type 
 * @returns 
 */
export function is(val: unknown, type: string) {
  return toString.call(val) === `[object ${type}]`
}


/**
 * 判断是否为函数
 * @param val 
 * @returns 
 */
export function isFunction(val: unknown): val is Function {
  return typeof val === 'function'
}

/**
 * 判断是否为字符串
 * @param val 
 * @returns 
 */
export function isString(val: unknown): val is string {
  return is(val, 'String')
}

/**
 * 判断是否为对象
 * @param val 
 * @returns 
 */
export function isObject(val: any): val is Record<any, any> {
  return val !== null && is(val, 'Object')
}

/**
 * 判断是否为空或未定义
 * @param val 
 * @returns 
 */
export function isNullOrUnDef(val: unknown) {
  return isUnDef(val) || isNull(val)
}

/**
 * 判断是否为空
 * @param val 
 * @returns 
 */
export function isNull(val: unknown) {
  return val === null
}

/**
 * 判断是否为未定义
 * @param val 
 * @returns 
 */
export function isUnDef<T = unknown>(val?: T): val is T {
  return !isDef(val);
}

/**
 * 判断是否为定义
 * @param val 
 * @returns 
 */
export function isDef<T = unknown>(val?: T): val is T {
  return typeof val !== 'undefined';
}
